﻿using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.ProjectTypeCommand;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.ProjectTypeCommandHander
{
    public class DelProjectTypeCommandHander : IRequestHandler<DelProjectTypeCommand, APIResult<int>>
    {
        private readonly ISqlSugarClient db;

        public DelProjectTypeCommandHander(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<APIResult<int>> Handle(DelProjectTypeCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();

            var list = await db.Queryable<ProjectType>().FirstAsync(x => x.ProjectTypeId == request.ProjectTypeId);

            list.Del = true;

            result.Data = await db.Updateable<ProjectType>(list).ExecuteCommandAsync();

            if (result.Data > 0)
            {
                result.Code = APIEnum.success;
                result.Msg = "删除成功";
            }
            else
            {
                result.Code = APIEnum.error;
                result.Msg = "删除失败";
            }

            return result;
        }
    }
}
